<template>
  <el-container>
    <el-header style="font-size: 12px;">
      <el-row type="flex" justify="space-between">
        <el-col :span="10" class="title">公司考勤管理系统</el-col>
        <el-col :span="6"></el-col>
        <el-col :span="2">
          <el-dropdown @command="handleCommand">
            <el-button class="avatar-button">
              <!-- 显示名字第一个字的圆形图标 -->
              <el-avatar size="small" style="background-color: rgba(28,64,94,0.22); margin-right: 5px;">
                {{ name.charAt(0) }}
              </el-avatar>
              <i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="myInfo">个人信息</el-dropdown-item>
              <el-dropdown-item command="logout" divided>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-header>

    <el-container style="height: calc(100vh - 120px);">
      <el-aside width="220px" style="display: block; background-color: #1a2a3a;">
        <el-menu
            router
            :default-openeds="[activeIndex]"
            unique-opened
            background-color="#1a2a3a"
            text-color="#b7c0cd"
            active-text-color="#ffd04b"
            @open="handleOpen"
            style="max-height: calc(100vh - 120px); overflow-y: auto; border-right: none;"
        >
          <!-- 首页 -->
          <el-menu-item index="/Mine/welcome">
            <i class="el-icon-s-home" style="color: #66b1ff;"></i>
            <span slot="title">首页</span>
          </el-menu-item>

          <!-- 我的 -->
          <el-menu-item index="/Mine/MyInfo">
            <i class="el-icon-user" style="color: #67c23a;"></i>
            <span slot="title">我的信息</span>
          </el-menu-item>

          <!-- 人事管理 -->
          <el-submenu index="4">
            <template slot="title">
              <i class="el-icon-s-custom" style="color: #e6a23c;"></i>
              <span>人事管理</span>
            </template>
            <el-menu-item index="/employee/AllEmployee" :disabled="isrenshi">
              <i class="el-icon-user-solid"></i>
              <span>人员管理</span>
            </el-menu-item>
            <el-menu-item index="/Department/DepartmentList" :disabled="isrenshi">
              <i class="el-icon-office-building"></i>
              <span>部门管理</span>
            </el-menu-item>
          </el-submenu>

          <!-- 加班与调休 -->
          <el-submenu index="8">
            <template slot="title">
              <i class="el-icon-time" style="color: #f56c6c;"></i>
              <span>加班与调休</span>
            </template>
            <el-menu-item index="/OverTime/All">
              <i class="el-icon-alarm-clock"></i>
              <span>加班申请</span>
            </el-menu-item>
            <el-menu-item index="/CompOff/All">
              <i class="el-icon-refresh-left"></i>
              <span>调休申请</span>
            </el-menu-item>
            <el-menu-item index="/Deal">
              <i class="el-icon-document-checked"></i>
              <span>申请处理</span>
            </el-menu-item>
          </el-submenu>

          <!-- 年休假 -->
          <el-submenu index="10">
            <template slot="title">
              <i class="el-icon-sunny" style="color: #e6a23c;"></i>
              <span>年休假管理</span>
            </template>
            <el-menu-item index="/Annualleave/AnnualleaveApply">
              <i class="el-icon-circle-plus"></i>
              <span>年休假申请</span>
            </el-menu-item>
          </el-submenu>

          <!-- 调级管理 -->
          <el-submenu index="11">
            <template slot="title">
              <i class="el-icon-s-promotion" style="color: #67c23a;"></i>
              <span>调级管理</span>
            </template>
            <el-menu-item index="/Level/LevelAdjustment">
              <i class="el-icon-sort-up"></i>
              <span>调级申请</span>
            </el-menu-item>
            <el-menu-item index="/Level/LevelAdjustmentRecord">
              <i class="el-icon-tickets"></i>
              <span>调级记录</span>
            </el-menu-item>
          </el-submenu>

          <!-- 数据统计 -->
          <el-menu-item index="/Data/LeaveDashboard">
            <i class="el-icon-data-line" style="color: #66b1ff;"></i>
            <span slot="title">综合数据统计</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-main style="padding: 20px; background-color: #f0f2f5;">
        <router-view></router-view>
      </el-main>
    </el-container>

    <el-footer>
      <div class="footer-content">
        <div class="footer-copyright">© 公司考勤管理系统</div>
        <div class="footer-links">
          <a href="javascript:void(0)" @click="navigateToWelcome">关于我们</a>
          <a href="javascript:void(0)" @click="navigateToWelcome">联系方式</a>
          <a href="javascript:void(0)" @click="navigateToWelcome">隐私政策</a>
        </div>
      </div>
    </el-footer>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      name: '',
      ismanager: true,
      isrenshi: true,
      iscaiwu: true,
      isCollapse: true,
      activeIndex: '3',
    };
  },
  created() {
    // 检查登录状态
    if (window.sessionStorage.getItem('empId') == null) {
      this.$router.replace({ path: '/login' });
    } else {
      this.name = window.sessionStorage.getItem('name');

      // 页面初始化时自动跳转到个人信息页面
      this.$router.push({name: '欢迎界面'});
    }

    if (window.sessionStorage.getItem('role') == 0 || window.sessionStorage.getItem('role') == 1|| window.sessionStorage.getItem('role') == 2) {
      this.ismanager = false;
      this.isrenshi = false;
      this.iscaiwu = false;
    }
  },
  methods: {
    handleCommand(command) {
      if (command === 'logout') {
        window.sessionStorage.removeItem('empId');
        window.sessionStorage.removeItem('name');
        this.$router.replace({ path: '/login' });
      }else if (command === 'myInfo') {
        // 跳转到个人信息页面
        this.$router.replace({ path: '/Mine/MyInfo' });
      }
    },
    navigateToWelcome() {
      this.$router.replace({name: '欢迎界面'});
    },
    handleOpen(key) {
      this.activeIndex = key;
    },
  },
};
</script>

<style scoped src="../../assets/style/home.css"></style>